package com.whpu.service.impl;

import com.whpu.mapper.AccountMapper;
import com.whpu.pojo.Account;
import com.whpu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by RiceBall on 2020/9/29 9:27
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void saveMoney(int id, double money) {
        Account account = accountMapper.findById(id);
        accountMapper.updateMoney(id, account.getMoney() + money);
    }

    @Override
    public void withdrawMoney(int id, double money) {
        Account account = accountMapper.findById(id);
        accountMapper.updateMoney(id, account.getMoney() - money);
    }

    @Override
    public void transferMoney(int idFrom, int idTo, double transferMoney) {
        Account accountFrom = accountMapper.findById(idFrom);
        accountMapper.updateMoney(idFrom, accountFrom.getMoney() - transferMoney);

        int i =1/0;

        Account accountTo = accountMapper.findById(idTo);
        accountMapper.updateMoney(idTo, accountTo.getMoney() + transferMoney);
    }
}
